﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace School.UserCentents
{
    public partial class Room : UserCheckPower
    {
        #region 分页相关

        //获取传入当前页码
        int PageIndex
        {
            get
            {
                if (Request.QueryString.Get("page") != null)
                {
                    if (!Common.TypeValidator.IsNumeric(Request.QueryString.Get("page")))
                    {
                        return 1;
                    }
                    return Convert.ToInt32(Request.QueryString.Get("page"));
                }
                return 1;
            }
        }

        //分页数
        private int PageSize
        {
            get
            {
                return 10;
            }
        }
        #endregion
        BLL.Users Users = new BLL.Users();
        BLL.Room Rooms = new BLL.Room();
        BLL.School Sch = new BLL.School();
        protected void Page_Load(object sender, EventArgs e)
        {
            Panel p1 = (Panel)Master.FindControl("Panel1");
            Panel p2 = (Panel)Master.FindControl("Panel2");
            Panel p3 = (Panel)Master.FindControl("Panel3");
            Panel p4 = (Panel)Master.FindControl("Panel4");
            p1.CssClass = "sldmenuoff";
            p2.CssClass = "sldmenuon0";
            p3.CssClass = "sld1";
            p4.CssClass = "sld0";
            p3.Attributes.Add("style", "display: none");
            p4.Attributes.Add("style", "");

            if (!IsPostBack)
            {
                PageLoad();
            }
        }

        private void PageLoad()
        {
            string strWhere = " and CreateUserID=" + LoginUserId;
            DataSet ds = Rooms.GetList("CreateUserID=" + LoginUserId);
            if (new BLL.Users().GetModel(LoginUserId).UserTypeID == 1)
            {
                XueXiaoClass.Visible = true;
                DropDownList1.DataSource = Sch.GetAllList();
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));
                strWhere = "";
                ds = Rooms.GetAllList();
            }
            if (new BLL.Users().GetModel(LoginUserId).UserTypeID == 2)
            {
                XueXiaoClass.Visible = true;
                DropDownList1.DataSource = Sch.GetList("CreateUserID=" + LoginUserId);
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));

                strWhere = " and Remark1=" + LoginUserId;
                ds = Rooms.GetList("Remark1=" + LoginUserId);
            }

            if (ds.Tables[0].Rows.Count > 0)
            {
                Repeater1.DataSource = Rooms.GetList(PageSize, PageIndex, strWhere);
                Repeater1.DataBind();
                AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
                AspNetPager1.PageSize = 10;
                AspNetPager1.CurrentPageIndex = PageIndex;
                AspNetPager1.DataBind();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue != "0")
            {
                string strWhere = " and SchoolId=" + DropDownList1.SelectedValue;
                DataSet ds = Rooms.GetList("SchoolId=" + DropDownList1.SelectedValue);
                Repeater1.DataSource = Rooms.GetList(PageSize, PageIndex, strWhere);
                Repeater1.DataBind();
                AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
                AspNetPager1.PageSize = 10;
                AspNetPager1.CurrentPageIndex = PageIndex;
                AspNetPager1.DataBind();
            }
            else
            {
                PageLoad();
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lb = (Label)e.Item.FindControl("Label1");
                Label lb2 = (Label)e.Item.FindControl("Label2");
                string[] s = lb.Text.Split(',');
                string a = "";
                if (s.Length == 2)
                {
                    a = Users.GetModel(int.Parse(s[0])).MemberName;
                }
                else
                {
                    for (int i = 0; i < s.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(s[i]))
                        {
                            a += Users.GetModel(int.Parse(s[i])).MemberName + " | ";
                        }
                    }
                }
                lb.Text = a;
                lb2.Text = Sch.GetModel(int.Parse(lb2.Text)).Name;
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                Response.Redirect("AddRoom.aspx?id=" + e.CommandArgument);
            }
            if (e.CommandName == "Del")
            {
                Rooms.Delete(int.Parse(e.CommandArgument.ToString()));
                PageLoad();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Model.Users u = new BLL.Users().GetModel(LoginUserId);
            string strWhere = " and Name like '%" + keyword.Text.Trim() + "%' and CreateUserID=" + LoginUserId;
            DataSet ds = Rooms.GetList("Name like '%" + keyword.Text.Trim() + "%' and CreateUserID=" + LoginUserId);
            if (u.UserTypeID == 1)
            {
                strWhere = " and Name like '%" + keyword.Text.Trim() + "%'";
                ds = Rooms.GetList("Name like '%" + keyword.Text.Trim() + "%'");
            }
            Repeater1.DataSource = Rooms.GetList(PageSize, PageIndex, strWhere);
            Repeater1.DataBind();
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            AspNetPager1.PageSize = 10;
            AspNetPager1.CurrentPageIndex = PageIndex;
            AspNetPager1.DataBind();
        }
    }
}